package com.wky.system.controller;

import com.wky.basic.Result.JsonResult;
import com.wky.system.pojo.SystemType;
import com.wky.system.service.SystemTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 数据字典
 */

@RestController
@RequestMapping("/system/type")
@Tag(name = "数据字典")
@CrossOrigin
public class SystemTypeController {

    @Autowired
    private SystemTypeService systemTypeService;

    /**
    * 查询所有品牌
     */
    @Operation(method ="查询所有品牌")
    @GetMapping("/findAll")
    public JsonResult findAll(){
        try {
            return JsonResult.success(systemTypeService.findAll());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     *  根据id查询品牌
     */
    @GetMapping("/findById")
    public JsonResult findById(Long id){
        try {
            return JsonResult.success(systemTypeService.findById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     *  分页查询品牌
     */
    @PostMapping("/findByPage")
    public JsonResult findByPage(@RequestBody SystemType systemType){
        try {
            return JsonResult.success(systemTypeService.findByPage(systemType));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     *  添加品牌
     */
    @PostMapping("/add")
    public JsonResult add(@RequestBody SystemType systemType){
        try {
            systemTypeService.add(systemType);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }

    }

    /**
     *  修改品牌
     */
    @PostMapping("/update")
    public JsonResult update(@RequestBody SystemType systemType){
        try {
            if (systemType.getId() == null){
                return JsonResult.error("品牌id不能为空");
            }
            systemTypeService.update(systemType);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     *  删除品牌
     */
    @DeleteMapping("/delete")
    public JsonResult delete(Long id){
        try {
            if (id == null){
                return JsonResult.error("品牌id不能为空");
            }
            systemTypeService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     *  批量删除品牌
     */
    @DeleteMapping("/deleteByIds")
    public JsonResult deleteBatch(@RequestBody Long[] ids){
        for (Long id : ids) {
            systemTypeService.delete(id);
        }
        return JsonResult.success();
    }

    /**
     * 根据typesId查询所有
     */
    @GetMapping("/findByTypesId")
    public JsonResult findByTypesId( Long typesId){
        try {
            return JsonResult.success(systemTypeService.findByTypesId(typesId));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

}
